﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using DAL;
using System.Data.SqlClient;
using System.Data;


public partial class Admin_Department_EditDepartmentSlogans : System.Web.UI.Page
{
    string path = HttpContext.Current.Server.MapPath("~/") + @"Admin\Upload\";
    string SQL_queryIntroduction = "SELECT * FROM [DS_IntroductionImg] WHERE DepartmentId=@DepartmentId ORDER BY OrderValue";
    string SQL_queryManagementSlogans = "SELECT * FROM [DS_ManagementSlogans] WHERE DepartmentId=@DepartmentId ORDER BY OrderValue";
    string SQL_queryCoreevents = "SELECT * FROM [DS_CoreeventsImg] WHERE DepartmentId=@DepartmentId ORDER BY OrderValue";
    string SQL_queryManagementObject = "SELECT * FROM [DS_ManagementObject] WHERE DepartmentId=@DepartmentId ORDER BY OrderValue";

    string SQL_insertIntroduction = "INSERT INTO  [DS_IntroductionImg]([DepartmentId],ImgUrl,[IsDefault],[OrderValue])VALUES(@DepartmentId,@ImgUrl,@IsDefault,@OrderValue)";
    string SQL_insertManagementSlogans = "INSERT INTO  [DS_ManagementSlogans]([DepartmentId],ImgUrl,[IsDefault],[OrderValue])VALUES(@DepartmentId,@ImgUrl,@IsDefault,@OrderValue)";
    string SQL_insertCoreevents = "INSERT INTO  [DS_CoreeventsImg]([DepartmentId],ImgUrl,[IsDefault],[OrderValue])VALUES(@DepartmentId,@ImgUrl,@IsDefault,@OrderValue)";
    string SQL_insertManagementObject = "INSERT INTO  [DS_ManagementObject]([DepartmentId],ImgUrl,[IsDefault],[OrderValue])VALUES(@DepartmentId,@ImgUrl,@IsDefault,@OrderValue)";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    /// <summary>
    /// 绑定图片列表
    /// </summary>
    void Bind()
    {
        int DepartmentId = HttpHelp.getParamsInt("DepartmentId");
        int typeid = HttpHelp.getParamsInt("typeid");
        SqlParameter[] parameters = new SqlParameter[1];
        parameters[0] = new SqlParameter("@DepartmentId", DepartmentId);
        switch (typeid)
        {
            case 1:
                this.gdvImgList.DataSource = SqlHelper.ExecuteDataset(SqlHelper.connStr,
CommandType.Text, SQL_queryIntroduction, parameters);
                break;
            case 2:
                this.gdvImgList.DataSource = SqlHelper.ExecuteDataset(SqlHelper.connStr,
CommandType.Text, SQL_queryCoreevents, parameters);
                break;
            case 3:
                this.gdvImgList.DataSource = SqlHelper.ExecuteDataset(SqlHelper.connStr,
CommandType.Text, SQL_queryManagementObject, parameters);
                break;
            case 4:
                this.gdvImgList.DataSource = SqlHelper.ExecuteDataset(SqlHelper.connStr,
CommandType.Text, SQL_queryManagementSlogans, parameters);
                break;
        }

        this.gdvImgList.DataBind();
    }

    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int DepartmentId = HttpHelp.getParamsInt("DepartmentId");
        int typeid = HttpHelp.getParamsInt("typeid");
        List<string> listfile = new List<string>();
        if (this.fupImg.HasFiles)
        {
            foreach (var file in this.fupImg.PostedFiles)
            {
                listfile.Add(file.FileName);
            }
            listfile.Sort();
            foreach (var item in this.fupImg.PostedFiles)
            {
                //判断是否存在
                if (System.IO.File.Exists(path + item.FileName))
                {//如果存在就删除已存在
                    System.IO.File.Delete(path + item.FileName);
                }
                item.SaveAs(path + item.FileName);


                string imgUrl = "/Admin/Upload/" + item.FileName;
                string isDefault = this.drpIsDefault.SelectedValue;
                int orderValue = Convert.ToInt32(this.txtOrderValue.Text);

                SqlParameter[] parameters = new SqlParameter[4];
                parameters[0] = new SqlParameter("@DepartmentId", DepartmentId);
                parameters[1] = new SqlParameter("@ImgUrl", imgUrl);
                parameters[2] = new SqlParameter("@IsDefault", isDefault);
                parameters[3] = new SqlParameter("@OrderValue", listfile.IndexOf(item.FileName) + 1);
                int result = 0;
                switch (typeid)
                {
                    case 1:
                        result = SqlHelper.ExecuteNonQuery(SqlHelper.connStr,
        CommandType.Text, SQL_insertIntroduction, parameters);
                        break;
                    case 2:
                        result = SqlHelper.ExecuteNonQuery(SqlHelper.connStr,
        CommandType.Text, SQL_insertCoreevents, parameters);
                        break;
                    case 3:
                        result = SqlHelper.ExecuteNonQuery(SqlHelper.connStr,
       CommandType.Text, SQL_insertManagementObject, parameters);
                        break;
                    case 4:
                        result = SqlHelper.ExecuteNonQuery(SqlHelper.connStr,
      CommandType.Text, SQL_insertManagementSlogans, parameters);
                        break;
                }
            }

            HttpHelp.alert("添加成功！", Page);
            Bind();
        }
        else
        {
            HttpHelp.alert("文件不存在，请重新选择！", Page);
        }

    }


    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引
        int Id = Convert.ToInt32(this.gdvImgList.DataKeys[_rowIndex]["Id"].ToString());
        string tablename = string.Empty;
        int typeid = HttpHelp.getParamsInt("typeid");
        switch (typeid)
        {
            case 1:
                tablename = "DS_IntroductionImg"; break;
            case 2:
                tablename = "DS_CoreeventsImg"; break;
            case 3:
                tablename = "DS_ManagementObject"; break;
            case 4:
                tablename = "DS_ManagementSlogans"; break;
        }
        object imgurl = null;
        string sql = "SELECT ImgUrl FROM " + tablename + " WHERE id=" + Id;
        imgurl = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, sql);
        sql = "DELETE " + tablename + " WHERE ID=" + Id;
        int result = SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql);
        if (result > 0)
        {
            HttpHelp.alert("删除成功！", Page);
            //判断是否存在
            if (System.IO.File.Exists(HttpContext.Current.Server.MapPath("~/") + imgurl))
            {//如果存在就删除已存在
                System.IO.File.Delete(HttpContext.Current.Server.MapPath("~/") + imgurl);
            }
            Bind();
        }
    }
    protected void btnDefault_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引
        int Id = Convert.ToInt32(this.gdvImgList.DataKeys[_rowIndex]["Id"].ToString());
        string tablename = string.Empty;
        int typeid = HttpHelp.getParamsInt("typeid");
        switch (typeid)
        {
            case 1:
                tablename = "DS_IntroductionImg"; break;
            case 2:
                tablename = "DS_CoreeventsImg"; break;
            case 3:
                tablename = "DS_ManagementObject"; break;
            case 4:
                tablename = "DS_ManagementSlogans"; break;
        }
        int DepartmentId = HttpHelp.getParamsInt("DepartmentId");
        string sql = "UPDATE " + tablename + " SET IsDefault=0 WHERE   DepartmentId=" + DepartmentId;
        SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql);
        sql = "UPDATE " + tablename + " SET IsDefault=1 WHERE id=" + Id;
        SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql);
        HttpHelp.alert("默认成功！", Page);
        Bind();
    }
}